<template>
  <div class="login">
    <el-card shadow="hover" class="box-card">
      <div slot="header" class="clearfix">
        <span>登录</span>
        <span v-if="isTrue" style="float: right; color: red">{{
          errorMsg
        }}</span>
      </div>
      <div class="text item">
        <el-form
          :rules="rules"
          label-position="left"
          label-width="80px"
          :model="loginUser"
        >
          <el-form-item label="账号" prop="username">
            <el-input v-model="loginUser.username"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input type="password" v-model="loginUser.password"></el-input>
          </el-form-item>
          <el-form-item label="验证码" prop="verifyCode">
            <el-input style="width: 75%" v-model="verifyCode"> </el-input>
            <VerifyVode
              ref="code"
              style="float: right"
              :changeCode.sync="identifyCode"
              identifyCodes="1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
              :contentHeight="40"
            ></VerifyVode>
          </el-form-item>
        </el-form>
      </div>
      <el-button
        style="float: right; margin-bottom: 20px"
        type="primary"
        @click="submitForm()"
        >登录</el-button
      >
    </el-card>
  </div>
</template>

<script>
import store from "@/store";
import user from "@/api/user";
import camerist from '@/api/camerist'
import VerifyVode from "@/components/VerifyVode.vue";
import jsCookie from 'js-cookie';

export default {
  components: {
    VerifyVode,
  },
  data() {
    return {
      loginUser: {},
      errorMsg: "",
      isTrue: false,
      verifyCode: "",
      identifyCode: "",
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            trigger: "blur",
          },
        ],
      },
    };
  },

  created() {
    // window.addEventListener('keydown', this.handkeyCode, true)
  },

  methods: {
    submitForm() {
      if (
        this.$store.state.user.verifyCode.toLowerCase() ===
        this.verifyCode.toLowerCase()
      ) {
        this.isTrue = false;
        this.errorMsg = "";
        user.login(this.loginUser).then((res) => {
          if (res.data.error_msg === "" || res.data.error_msg === undefined) {
            this.$message({
              message: "登录成功",
              type: "success",
            });
            jsCookie.set("user", JSON.stringify(res.data.user), {expires: 7})
            this.isTrue = false;
            store.commit("updateUser", res.data.user);
            store.commit("updateLoginStatus", true);
            this.getCamerist()
            console.log(this.$store.state.user)
            this.$router.push({ path: "/index" });
          } else {
            this.isTrue = true;
            this.loginUser = {};
            this.errorMsg = '账号或密码错误'
          }
        });
      } else {
        this.errorMsg = "验证码错误";
        this.isTrue = true;
        this.verifyCode = "";
        this.$refs.code.changeCode();
      }
    },
    getCamerist() {
      camerist.getApplyByUserId(this.$store.state.user.userId).then( res => {
        if ( res.success ) {
          store.commit("setCamerist", res.data.camerist)
        }
      })
    }
  },
};
</script>

<style scoped>
.login {
  width: 40%;
  margin-left: 30%;
  margin-right: 30%;
  margin-top: 5%;
}
.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}
.clearfix:after {
  clear: both;
}
</style>